<?php
    
extract(extractPost(), EXTR_PREFIX_ALL, 'c');
if(isset($c_column_id)){
    $column = R::load( 'column', $c_column_id );
    if($column->id == 0){
        jsonErrorDie("Column with provided id does not exist.");
    }
    if(isset($c_name) && $c_name != $column->name){
        if(R::count('column',' board_id = :board_id AND name = :name ', [":board_id" => $column->board_id, ":name" => $c_name]) > 0){
            jsonErrorDie("Column with provided name: " . $c_name . " already exists in table.");
        }
        
        $columns = R::findAll('column',' board_id = :board_id AND name = :name ', [":board_id" => $column->board_id, ":name" => $column->name]);
        
        foreach ($columns as $col){
            if($col->id == $column->id){
                $column->name = $c_name;
            }
            $col->name = $c_name;
            R::store( $col );
        }
    }
    
    if(isset($c_subcolumn) && !is_null($c_subcolumn) && $c_subcolumn != $column->subcolumn){
        
        if(R::count('column',' board_id = :board_id AND name = :name AND subcolumn = :subcolumn', 
                        [":board_id" => $column->board_id, ":name" => $column->name, ":subcolumn" => $c_subcolumn]) > 0){
            
            jsonErrorDie("Column with provided name: " . $c_name . " already exists in table.");
        }
        
        $column->subcolumn = $c_subcolumn;
    }
    
    if(isset($c_priority) && $c_priority <=2 && $c_priority>=0){
        
        if($c_priority == 2 && R::count('column', 'priority = 2 AND board_id = ?', array($column->board_id)) > 0){
            jsonErrorDie("Column with high priority already exists in table.");
        }   
        $column->priority = $c_priority;
            
    }
    
    // Checking for left and right most column
    // $b_boundary: -1 = leftmost, 1 = rightmost, else normal
    if(isset($c_boundary) && abs($c_boundary) == 1 && $c_boundary != $column->boundary){
        $boundary_exists = R::findOne('column',' board_id = :board_id AND boundary = :boundary', [":board_id" => $column->board_id, ":boundary" => $c_boundary]);
        if($boundary_exists){
            jsonErrorDie( (($c_boundary == -1)?"Left":"Right") . "most column is already defined (". $boundary_exists->name .").");
        }
        $column->boundary = $c_boundary;
        
    }
    if(isset($c_boundary) && $c_boundary == 0){
        $column->boundary = 0;
    }
    
    $wipViolation = false;
    if(isset($c_wip)){
        $column->wip = $c_wip;
        // Check for wip violations
        if(R::count('card', 'column_id = ?', array($c_column_id)) > $c_wip){
            $wipViolation = true;
        }
    }
    
    R::store($column);
    if($wipViolation){
        jsonSuccessDie("wipViolation", "Edited column contains more cards as new WIP allows!");
    }
    else{
        jsonSuccessDie("msg", "Alles in Ordnung.");
    }
}
else{
    jsonErrorDie("Please provide column id.");
}
